package dashboard.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * 时间日期工具类
 * 
 * <p>
 * detailed comment
 * 
 * @author guolu 2015-1-24
 * @see
 * @since 1.0
 */
public class DDateUtils {

	private static Log log = LogFactory.getLog(DDateUtils.class);

	private static SimpleDateFormat Formator_day = new SimpleDateFormat("yyyyMMdd");

	/**
	 * 时间跨度
	 * 
	 * @param begin
	 * @param end
	 * @return
	 * @author guolu 2015-1-24
	 */
	public static List<String> getDays(String begin, String end) {
		Date beginDate = null;
		Date endDate = null;
		try {
			beginDate = Formator_day.parse(begin);
			endDate = Formator_day.parse(end);
		} catch (ParseException e) {
			log.info(e);
		}
		if (beginDate == null || endDate == null) {
			return Collections.emptyList();
		}

		Calendar c = Calendar.getInstance();
		c.setTime(beginDate);
		List<String> result = new ArrayList<String>();
		while (c.getTime().before(endDate)) {
			result.add(Formator_day.format(c.getTime()));
			c.add(Calendar.DAY_OF_MONTH, 1);
		}

		return result;
	}

	public static String formatDay(Date date) {
//		log.error(date);
		String day = Formator_day.format(date);
		return day;
	}

	/**
	 * 按时间查询的起始时间-日
	 * 
	 * @return
	 * @author guolu 2015-1-26
	 */
	public static String getBeginDay() {
		return "20140101";
	}

	/**
	 * 按时间查询的结束时间-日
	 * 
	 * @return
	 * @author guolu 2015-1-26
	 */
	public static String getEndDay() {
		return formatDay(getYesterday());
	}

	/**
	 * 昨日时间
	 * 
	 * @return
	 * @author guolu 2015-1-26
	 */
	public static Date getYesterday() {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(System.currentTimeMillis());
		c.add(Calendar.DAY_OF_MONTH, -1);
		return DateUtils.truncate(c, Calendar.DATE).getTime();
	}

	/**
	 * 
	 * @param date
	 * @return
	 * @author guolu 2015-1-26
	 */
	public static Date getLastDay(Date date) {
		return DateUtils.addDays(date, -1);
	}

	/**
	 * 返回某日的上周--定义为减去8天
	 * 
	 * @param date
	 * @return
	 * @author guolu 2015-1-26
	 */
	public static Date getLastWeek(Date date) {
		return DateUtils.addDays(date, -7);
	}

	/**
	 * 返回某日的上个月--定义为减去31天
	 * 
	 * @param date
	 * @return
	 * @author guolu 2015-1-26
	 */
	public static Date getLastMonth(Date date) {
		return DateUtils.addDays(date, -31);
	}

	public static String getLastDay(String date) {
		try {
			return Formator_day.format(DateUtils.addDays(Formator_day.parse(date), -1));
		} catch (ParseException e) {
			log.info(e);
		}
		return null;
	}

	public static Date parseDate(String date) {
		try {
			return Formator_day.parse(date);
		} catch (ParseException e) {
			log.info(e);
		}
		return null;
	}
}
